<?php

/**
 *
 * 版权所有：恰维网络<qwadmin.qiawei.com>
 * 作    者：寒川<hanchuan@qiawei.com>
 * 日    期：2016-01-20
 * 版    本：1.0.0
 * 功能说明：用户控制器。
 *
 * */

namespace Qwadmin\Controller;

class ExcelController extends ComController {
	
	/**
	* 提现申请下载
	**/
	public function cashapply(){
		$prefix = C('DB_PREFIX');
		$uid = I("uid" , 0 , 'intval');//获取需要导出下级帐号的ID
        $user_gid=$this->USER;//获取登陆账户的权限
		
		if(empty($this->USER)){
			die('未登录，不可下载表格');
		}
		if($this->USER['level']!=1){
			die('无权操作');
		}
		if(empty($uid)){
			$member_ret = M('member')->field('uid')->where("level=3")->select();
			$uid = [];
			foreach($member_ret as $value){
				$uid[]=$value['uid'];
			}
		}
		$start_date = htmlspecialchars(I("start_date" , '') , ENT_NOQUOTES);
		$end_date = htmlspecialchars(I("end_date" , '') , ENT_NOQUOTES);
		$current_timestamp   = (!empty($end_date))?$end_date:date('Y-m-d');
        $yesterday_timestamp = (!empty($start_date))?$start_date:date('Y-m-d',strtotime($current_timestamp)-(24*3600));
		
		$where = [
					$prefix . 'member.uid'			=> is_array($uid)?['in',$uid]:$uid,
					$prefix . 'withdraw_log.status'	=> 0
				];
		if(!empty($start)){
			$where['create_time'][] = [
				'EGT',
				strtotime($current_timestamp)
			];
		}
		if(!empty($end)){
			$where['create_time'][] = [
				'LT',
				strtotime($yesterday_timestamp)
			];
		}
		//获取帐号下的提现申请
		$log_ret = M('withdraw_log')->field($prefix . 'withdraw_log.*,'.$prefix . 'member.user')
									->join($prefix . 'member on ' . $prefix . 'member.uid = ' . $prefix . 'withdraw_log.uid')
									->where($where)
									->select();
		//定义表头
		$str = "会员ID\t会员名称\t打款帐号类型\t帐号名称\t打款账号\t应付金额\t\n";
        $str = iconv('utf-8','gb2312',$str);
		$filename = iconv('utf-8', 'gbk//IGNORE', $yesterday_timestamp."--".$current_timestamp."--{$log_ret['user']}-提现申请表.xls");
		
		foreach ($log_ret as $key => $v) {
			$str .= iconv('utf-8', 'gbk//IGNORE', $v['uid']) . "\t";
			$v['user'] = str_replace("—", "-", $v['user']);
			$v['user'] = str_replace("-", "-", $v['user']);
			$str .= iconv('utf-8', 'gbk//IGNORE', $v['user']) . "\t";
			$str .= iconv('utf-8', 'gbk//IGNORE', $v['account_type']==1?'支付宝':($v['account_type']==2?'微信':'银行卡')) . "\t";
			$str .= iconv('utf-8', 'gbk//IGNORE', $v['realname']) . "\t";
			$str .= iconv('utf-8', 'gbk//IGNORE', $v['alipay']) . "\t";
			$str .= iconv('utf-8', 'gbk//IGNORE', sprintf('%.2f',$v['amount'])) . "\t\n";
        }

        $this->exportExcel($filename,$str);
		
	}
	
	
	/**
	* 导出结算表
	**/
	public function download_excel(){

        header("Content-type: text/html; charset=utf-8");
        set_time_limit(0);

        $prefix = C('DB_PREFIX');
		$uid = I("uid" , 0 , 'intval');//获取需要导出下级帐号的ID
        $user_gid=$this->USER;//获取登陆账户的权限
		
		if(empty($user_gid['level'])){
			die('未登录，不可下载表格');
		}
		if(empty($uid)){
			die('未确定上级账户，不可下载');
		}
        
		$start_date = htmlspecialchars(I("start_date" , '') , ENT_NOQUOTES);
		$end_date = htmlspecialchars(I("end_date" , '') , ENT_NOQUOTES);
		$current_timestamp   = (!empty($end_date))?$end_date:date('Y-m-d');
        $yesterday_timestamp = (!empty($start_date))?$start_date:date('Y-m-d',strtotime($current_timestamp)-(24*3600));
		
		//定义表头
		$str = "会员ID\t会员名称\t打款帐号类型\t帐号名称\t打款账号\t收入金额\t分成比例\t应付金额\t\n";
        $str = iconv('utf-8','gb2312',$str);
		
		//获取帐号的信息
        $member = M('member')->where("uid={$uid}")->find();
		//获取帐号包含其下级的总收益
		$pay_money = D('Income')->Income_count($member['uid'],$member['level'],$yesterday_timestamp,$current_timestamp);
		
		$str .= iconv('utf-8', 'gbk//IGNORE', $member['uid']) . "\t";
		$member['user'] = str_replace("—", "-", $member['user']);
		$member['user'] = str_replace("-", "-", $member['user']);
		$str .= iconv('utf-8', 'gbk//IGNORE', $member['user']) . "\t";
		$str .= iconv('utf-8', 'gbk//IGNORE', $member['account_type']==1?'支付宝':($member['account_type']==2?'微信':'银行卡')) . "\t";
		$str .= iconv('utf-8', 'gbk//IGNORE', $member['truename']) . "\t";
		$str .= iconv('utf-8', 'gbk//IGNORE', $member['alipay']) . "\t";
		$str .= iconv('utf-8', 'gbk//IGNORE', sprintf('%.2f',$pay_money)) . "\t";
		$str .= iconv('utf-8', 'gbk//IGNORE', $member['proportion']) . "%\t";
		$str .= iconv('utf-8', 'gbk//IGNORE', sprintf('%.2f',$pay_money*($member['proportion']/100))) . "\t\n\n";
		$str .= iconv('utf-8', 'gbk//IGNORE', '下线分成') . "\t\n";
		
		
		$filename = iconv('utf-8', 'gbk//IGNORE', $yesterday_timestamp."--".$current_timestamp."--{$member['user']}-超高转化收益数据表.xls");

		
		//获取帐号创建的所有下级
		$where = "{$prefix}member.pid={$uid}";
        $user = M('member');
        $list  = $user->field("{$prefix}member.*")
						->where($where)
						->order('uid desc')
						->select();

        foreach ($list as $key => $v) {
			$pay_money = D('Income')->Income_count($v['uid'],$v['level'],$yesterday_timestamp,$current_timestamp);
			$str .= iconv('utf-8', 'gbk//IGNORE', $v['uid']) . "\t";
			$v['user'] = str_replace("—", "-", $v['user']);
			$v['user'] = str_replace("-", "-", $v['user']);
			$str .= iconv('utf-8', 'gbk//IGNORE', $v['user']) . "\t";
			$str .= iconv('utf-8', 'gbk//IGNORE', $v['account_type']==1?'支付宝':($v['account_type']==2?'微信':'银行卡')) . "\t";
			$str .= iconv('utf-8', 'gbk//IGNORE', $v['truename']) . "\t";
			$str .= iconv('utf-8', 'gbk//IGNORE', $v['alipay']) . "\t";
			$str .= iconv('utf-8', 'gbk//IGNORE', sprintf('%.2f',$pay_money)) . "\t";
			$str .= iconv('utf-8', 'gbk//IGNORE', $v['proportion']) . "%\t";
			$str .= iconv('utf-8', 'gbk//IGNORE', sprintf('%.2f',$pay_money*($v['proportion']/100))) . "\t\n";
        }

        $this->exportExcel($filename,$str);
    }

	private  function exportExcel($filename,$content){
		header("Pragma: public");
		header("Expires: 0");
		header("Cache-Control:must-revalidate,post-check=0,pre-check=0");
		header("Content-Type:application/force-download");
		header("Content-Type:application/vnd.ms-execl");
		header("Content-Type:application/octet-stream");
		header("Content-Type:application/download");
		header("Content-Disposition: attachment; filename=".$filename);
		header("Content-Transfer-Encoding:binary");
		echo $content;
	}
}
